﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Settings;
using Utils;
using System.Data;
using System.Data.SqlClient;

namespace PanasonicWarranty.Business
{
    public partial class pa_AgencyPhone
    {
        #region private field
        DBWarrantyDataContext dbcontext = new DBWarrantyDataContext(AppSetting.ConnectionString);
        #endregion

        #region Singleton Pattern
        private static object _lock = new object();
        private static List<pa_AgencyPhone> _instance;
        public static List<pa_AgencyPhone> GetInstance()
        {
            lock (_lock)
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        using (var context = new DBWarrantyDataContext(AppSetting.ConnectionString))
                        {
                            _instance = context.pa_AgencyPhones.ToList();
                        }
                    }
                }
            }
            return _instance;
        }
        public static void ReloadData()
        {
            lock (_lock)
            {
                using (var context = new DBWarrantyDataContext(AppSetting.ConnectionString))
                {
                    _instance = context.pa_AgencyPhones.ToList();
                }
            }
        }
        #endregion Singleton Pattern
      
        public static bool Delete(int AgencyID)
        {
            try
            {
                string query = "DELETE FROM pa_AgencyPhone WHERE AgencyID = @AgencyID";
                return (SqlHelper.ExecuteNonQuery(AppSetting.ConnectionString, CommandType.Text, query, new SqlParameter("@AgencyID", AgencyID)) > 0);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool Create()
        {
            dbcontext.pa_AgencyPhones.InsertOnSubmit(this);
            dbcontext.SubmitChanges();
            return true;
        }
    }
}
